package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.entity.Page;
import cn.group7.padserver.pojo.Order;
import cn.group7.padserver.service.OrderService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 谷汉斌
 * @description 订单类业务接口
 * @createTime 2024/7/22 下午3:00
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 随机返回10条记录
     * GET请求：http://localhost:8090/hotOrders
     *
     * @return 响应数据
     */
    @GetMapping("/hotOrders")
    public Result queryOrdersRandom() {
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(Code.QUERY_SUCCESS.getCode())
                .data(orderService.queryOrdersRandom())
                .message(Code.QUERY_SUCCESS.getInfo())
                .build();
    }


    /**
     * 分页展示所有商品（订单）
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param flag     查询类型
     * @return 商品列表
     */
    @ApiOperation(value = "分页展示所有商品（订单）")
    @GetMapping("/all")
    public Result selectOrders(@RequestParam("pageNum") Integer pageNum
            , @RequestParam(value = "flag", defaultValue = "all") String flag, @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        Page<Order> page = orderService.getOrdersByPage(pageNum, pageSize, flag);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(page)
                .flag(Flag.SUCCESS.isCode()).build();
    }

    /**
     * 分页查询关键字商品（订单）
     *
     * @param pageNum 页码
     * @return 商品列表
     */
    @ApiOperation(value = "分页查询关键字商品（货源）")
    @GetMapping("/selectByKeys")
    public Result selectByKeys(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                               @RequestParam("keys") String keys,
                               @RequestParam(value = "flag", defaultValue = "all") String flag) {
        Page<Order> page = orderService.selectByKeys(pageNum, pageSize, keys, flag);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(page)
                .flag(Flag.SUCCESS.isCode())
                .build();
    }

    /**
     * 根据id查询商品（订单）
     *
     * @param orderId 订单Id
     * @return Result
     */
    @ApiOperation(value = "根据id查询商品（订单）")
    @GetMapping("/selectOrderById/{orderId}")
    public Result selectOrderById(@PathVariable("orderId") Integer orderId) {
        Order order = orderService.queryOrderById(orderId);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(order)
                .flag(Flag.SUCCESS.isCode())
                .build();
    }

    /**
     * 根据用户名查询商品（订单）
     */
    @ApiOperation(value = "根据用户名查询商品（订单）")
    @GetMapping("/selectByUsername")
    public Result selectByUsername(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                   @RequestParam(value = "keys", defaultValue = "") String keys,
                                   @RequestParam(value = "flag", defaultValue = "all") String flag) {
        Page<Order> page = orderService.queryOrderByUsername(pageNum, pageSize, keys, flag);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(page)
                .flag(Flag.SUCCESS.isCode()).build();
    }


    /**
     * 添加商品（订单）
     */
    @ApiOperation(value = "添加商品（订单）")
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order,
                           @RequestParam(value = "flag", defaultValue = "goods") String flag) {
        boolean add = orderService.addOrder(order, flag);
        return Result.builder().code(add ? Code.ADD_SUCCESS.getCode() : Code.ADD_FAIL.getCode())
                .message(add ? Code.ADD_SUCCESS.getInfo() : Code.ADD_FAIL.getInfo())
                .flag(add ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .build();
    }

    /**
     * 修改商品（订单）
     */
    @ApiOperation(value = "修改商品（订单）")
    @PutMapping("/updateOrder")
    public Result updateOrder(@RequestBody Order order,
                              @RequestParam(value = "flag", defaultValue = "goods") String flag) {
        System.out.println(order);
        boolean update = orderService.updateOrder(order, flag);
        return Result.builder().code(update ? Code.UPDATE_SUCCESS.getCode() : Code.UPDATE_FAIL.getCode())
                .message(update ? Code.UPDATE_SUCCESS.getInfo() : Code.UPDATE_FAIL.getInfo())
                .flag(update ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode()).build();
    }


    /**
     * 删除商品（订单）
     */
    @ApiOperation(value = "删除商品（订单）")
    @DeleteMapping("/deleteOrder/{orderId}")
    public Result deleteOrder(@PathVariable("orderId") Integer orderId) {
        boolean delete = orderService.deleteOrder(orderId);
        return Result.builder().code(delete ? Code.DELETE_SUCCESS.getCode() : Code.DELETE_FAIL.getCode())
                .message(delete ? Code.DELETE_SUCCESS.getInfo() : Code.DELETE_FAIL.getInfo())
                .flag(delete ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode()).build();
    }
}
